﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using QLHD.Models;

namespace QLHD.Handlers
{
    /// <summary>
    /// Summary description for post
    /// </summary>
    public class post : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            if (!context.User.Identity.IsAuthenticated)
                return;

            context.Response.ContentType = "text/plain";
            string act = context.Request.Params["act"];
            switch (act.ToLower())
            {
                case "xcontract":
                    DeleteContract(context);
                    break;
                case "xkcp":
                    DeleteKCP(context);
                    break;
                case "xxnkl":
                    DeleteXNKL(context);
                    break;
                case "xuser":
                    DeleteUser(context);
                    break;
                case "xgroups":
                    DoDelete(context);
                    break;
                case "phongban_del":
                    DoDeletePhongBan(context);
                    break;


            }
            //if (act.ToLower() == "xcontract")
            //{
            //    DeleteContract(context);
            //}
            //else if (act.ToLower() == "xkcp")
            //{
            //    DeleteKCP(context);
            //}
            //else if (act.ToLower() == "xxnkl")
            //{
            //    DeleteXNKL(context);
            //}
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private void DeleteContract(HttpContext context)
        {
            int mahd = int.Parse(context.Request["MaHD"]);
            QLHD2013Entities db = new QLHD2013Entities();
            var hd = db.HopDongs.Where(q => q.MaHD == mahd).FirstOrDefault();
            if (hd != null)
            {
                hd.TrangThaiHD = 0;
                db.SaveChanges();
            }
            context.Response.Write("success");
        }

        private void DeleteXNKL(HttpContext context)
        {
            try
            {
                int mahd = int.Parse(context.Request["MaXNKL"]);
                QLHD2013Entities db = new QLHD2013Entities();
                var hd = db.XNKLs.Where(q => q.MaXNKL == mahd).FirstOrDefault();
                if (hd != null)
                {
                    db.DeleteObject(hd);
                    db.SaveChanges();
                }
                context.Response.Write("success");
            }
            catch
            {
                context.Response.Write("error");
            }
        }

        private void DeleteKCP(HttpContext context)
        {
            try
            {
                int mahd = int.Parse(context.Request["MaKCP"]);
                QLHD2013Entities db = new QLHD2013Entities();
                var hd = db.KhoanChiPhis.Where(q => q.MaKCP == mahd).FirstOrDefault();
                if (hd != null)
                {
                    db.DeleteObject(hd);
                    db.SaveChanges();
                }
                context.Response.Write("success");
            }
            catch
            {
                context.Response.Write("error");
            }
        }
        private void DeleteUser(HttpContext context)
        {
            int id = int.Parse(context.Request["ID"]);
            QLHD2013Entities db = new QLHD2013Entities();
            var user = db.Users.Where(q => q.ID == id).FirstOrDefault();
            if (user != null)
            {
                //db.DeleteObject(user);
                user.TrangThai = false;
                db.SaveChanges();
            }
            context.Response.Write("success");
        }
        private void DoDelete(HttpContext context)
        {
            int id = int.Parse(context.Request["ID"]);
            QLHD2013Entities db = new QLHD2013Entities();
            var obj = db.Groups.Where(q => q.ID == id).FirstOrDefault();
            if (obj != null)
            {
                db.DeleteObject(obj);
                db.SaveChanges();
            }
            context.Response.Write("success");
        }
        private void DoDeletePhongBan(HttpContext context)
        {
            int id = int.Parse(context.Request["ID"]);
            QLHD2013Entities db = new QLHD2013Entities();
            var obj = db.PhongBans.Where(q => q.ID == id).FirstOrDefault();
            if (obj != null)
            {
                obj.TrangThai = false;
                //db.DeleteObject(obj);
                db.SaveChanges();
            }
            context.Response.Write("success");
        }
    }
}